package mydsl;

import java.util.Iterator;

public abstract class AbstractContainer<E> implements Container<E> {
	public abstract int size();
	public abstract Iterator<E> iterator();
	
	public boolean isEmpty() {
		return size() == 0;
	}

	public boolean contains(E o) {
		Iterator<E> e = iterator();
		if (o==null) {
		    while (e.hasNext())
			if (e.next()==null)
			    return true;
		} else {
		    while (e.hasNext())
			if (o.equals(e.next()))
			    return true;
		}
		return false;
	}
	
	public String toString() {
        Iterator<E> i = iterator();
        return toString(i);
    }
	
	public String toString(Iterator<E> i) {
        if (!i.hasNext())
        	return "[]";

		StringBuilder sb = new StringBuilder();
		sb.append('[');
		for (;;) {
		    E e = i.next();
		    sb.append(e == this ? "(this Collection)" : e);
		    if (! i.hasNext())
		    	return sb.append(']').toString();
		    sb.append(", ");
		}
    }
}
